package com.clsz.controller;

import com.clsz.bean.bo.OrdersearchBo;
import com.clsz.service.OrderService;
import com.clsz.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Resource
    private RedisUtils redisUtils;

    /**
     * 生成订单接口
     *
     * @param authrization
     * @param ordersearchBo
     * @return
     */
    @PutMapping
    public Object addOrder(@RequestHeader String authorization,
                           @RequestBody OrdersearchBo ordersearchBo) {
        System.out.println("========================");
        System.out.println(ordersearchBo);
        Map<String, Object> res = new HashMap<>();
        //身份验证
        if (redisUtils.hasKey(authorization)) {
            if (orderService.addOrder(ordersearchBo, authorization)) {
                res.put("code", "200");
                res.put("message", "ok");
            } else {
                res.put("code", "500");
                res.put("message", "订单失败");
            }
        } else {
            res.put("code", "401");
            res.put("message", "Unauthrization");
        }

        return res;
    }
}
